package com.runlooker.runer.dao;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.runlooker.runer.db.RunerDBOpenHelper;
import com.runlooker.runer.db.RunerTableSql;
import com.runlooker.runer.entity.PersonInfoEntity;

public class PersonInfoDao {
	private final String tag = PersonInfoDao.class.getSimpleName();
	private String mTable = RunerTableSql.PERSON_INFO_ENTRY_TABLE;

	private RunerDBOpenHelper mRunerDBOpenHelper;

	public PersonInfoDao(Context context) {
		mRunerDBOpenHelper = new RunerDBOpenHelper(context);
	}

	/**
	 * 判断联系人信息是否存在数据库中
	 * 
	 * @param account
	 * @param db
	 * @return
	 */
	private boolean IsExists(String account, SQLiteDatabase db) {
		boolean flag = false;
		String[] args = { account };
		Cursor cursor = db.query(mTable, null, "account = ?", args, null, null,
				null);
		if (cursor != null && cursor.getCount() > 0) {
			flag = true;
		} else {
			flag = false;
		}
		cursor.close();
		return flag;
	}

	// public boolean IsExists(String account) {
	// SQLiteDatabase db = mRunerDBOpenHelper.getReadableDatabase();
	// boolean flag = IsExists(account, db);
	// // db.close();
	// return flag;
	// }

	/**
	 * 
	 * 清空表
	 * 
	 * @Title: clearTable
	 * @Description: 方法描述
	 */
	public synchronized void clearTable() {
		SQLiteDatabase db = mRunerDBOpenHelper.getReadableDatabase();
		StringBuffer updateSql = new StringBuffer("DELETE FROM " + mTable);
		db.execSQL(updateSql.toString());
		// db.close();
	}

	/**
	 * 
	 * 根据帐号获得详细信息
	 * 
	 * @param account
	 * @return info 或 null
	 */
	public synchronized PersonInfoEntity getPersonInfo(String account) {
		SQLiteDatabase db = mRunerDBOpenHelper.getReadableDatabase();
		PersonInfoEntity info = new PersonInfoEntity();
		String[] args = { account };
		Cursor cursor = db.query(mTable, null, "account = ?", args, null, null,
				null);
		if (cursor != null && cursor.getCount() > 0 && cursor.moveToNext()) {
			info.setAccount(cursor.getString(cursor.getColumnIndex("account")));
			info.setAddress(cursor.getString(cursor.getColumnIndex("address")));
			info.setCtry(cursor.getString(cursor.getColumnIndex("ctry")));
			info.setEmail(cursor.getString(cursor.getColumnIndex("email")));
			info.setField(cursor.getString(cursor.getColumnIndex("field")));
			info.setFullName(cursor.getString(cursor.getColumnIndex("fullName")));
			info.setNickName(cursor.getString(cursor.getColumnIndex("nickName")));
			info.setOrg(cursor.getString(cursor.getColumnIndex("org")));
			info.setPhoneNum(cursor.getString(cursor.getColumnIndex("phoneNum")));
		} else {
			cursor.close();
			return null;
		}
		cursor.close();
		// db.close();
		return info;
	}

	/**
	 * 
	 * 删除指定帐号的联系人信息
	 * 
	 * @param account
	 */
	public synchronized void deleInfoByAccount(String account) {
		SQLiteDatabase db = mRunerDBOpenHelper.getReadableDatabase();
		String sqlString = "DELETE FROM " + mTable + " WHERE account = " + "'"
				+ account + "'";
		db.execSQL(sqlString);
		// db.close();
	}

	private synchronized void savePersonInfo(PersonInfoEntity info,
			SQLiteDatabase db) {
		if (info == null) {
			return;
		}
		ContentValues cv = new ContentValues();
		cv.put("account", info.getAccount());
		cv.put("fullName", info.getFullName());
		cv.put("nickName", info.getNickName());
		cv.put("org", info.getOrg());
		cv.put("email", info.getEmail());
		cv.put("phoneNum", info.getPhoneNum());
		cv.put("ctry", info.getCtry());
		cv.put("address", info.getAddress());
		cv.put("field", info.getField());
		db.insert(mTable, null, cv);
	}

	/**
	 * 
	 * 更新或保存信息
	 * 
	 * @param info
	 */
	public synchronized void saveOrUpdatePersonInfo(PersonInfoEntity info) {
		if (info == null) {
			return;
		}
		SQLiteDatabase db = mRunerDBOpenHelper.getReadableDatabase();
		if (IsExists(info.getAccount().trim(), db)) {
			updatePersonInfo(info, db);
		} else {
			savePersonInfo(info, db);
		}
		// db.close();
	}

	/**
	 * 
	 * 更新或保存个人信息列表
	 * 
	 * @param list
	 */
	public synchronized void saveOrUpdatePersonInfoList(
			List<PersonInfoEntity> list) {

		if (list == null || list.isEmpty()) {
			return;
		}
		SQLiteDatabase db = mRunerDBOpenHelper.getReadableDatabase();
		for (PersonInfoEntity info : list) {
			if (IsExists(info.getAccount(), db)) {
				updatePersonInfo(info, db);
			} else {
				savePersonInfo(info, db);
			}
		}
		// db.close();
	}

	/**
	 * 
	 * 更新个人信息
	 * 
	 * @param info
	 * @param db
	 */
	private synchronized void updatePersonInfo(PersonInfoEntity info,
			SQLiteDatabase db) {
		if (info == null) {
			return;
		}
		ContentValues cv = new ContentValues();

		cv.put("fullName", info.getFullName());
		cv.put("nickName", info.getNickName());
		cv.put("org", info.getOrg());
		cv.put("email", info.getEmail());
		cv.put("phoneNum", info.getPhoneNum());
		cv.put("ctry", info.getCtry());
		cv.put("address", info.getAddress());
		cv.put("field", info.getField());

		String[] args = { info.getAccount() };
		db.update(mTable, cv, "account = ?", args);
	}
}
